#include <lua.hpp>

#include "add.hpp"
#include "div.hpp"
#include "mul.hpp"
#include "sub.hpp"

int add(lua_State* L) {
    double a = lua_tonumber(L, 1);
    double b = lua_tonumber(L, 2);
    lua_pushnumber(L, add(a, b));
    return 1;
}

int sub(lua_State* L) {
    double a = lua_tonumber(L, 1);
    double b = lua_tonumber(L, 2);
    lua_pushnumber(L, sub(a, b));
    return 1;
}

int mul(lua_State* L) {
    double a = lua_tonumber(L, 1);
    double b = lua_tonumber(L, 2);
    lua_pushnumber(L, mul(a, b));
    return 1;
}

int div(lua_State* L) {
    double a = lua_tonumber(L, 1);
    double b = lua_tonumber(L, 2);
    lua_pushnumber(L, div(a, b));
    return 1;
}

extern "C" {
int luaopen_libraries_operators(lua_State* L) {
    constexpr luaL_Reg functions[] = {
        {"add", add}, {"sub", sub}, {"mul", mul}, {"div", div}, {NULL, NULL}};
    luaL_register(L, "operators", functions);
    return 1;
}
}
